package com.atguigu.gmall.payment.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-06-27 15:57
 */
@RestController
@RequestMapping("/api/payment/")
public class AlipayController {


    @Autowired
    private AlipayService alipayService;

    /**
     * 渲染支付页面
     *
     * @param orderId
     * @return
     */
    @GetMapping("/alipay/submit/{orderId}")
    public String alipayPage(@PathVariable("orderId") Long orderId) {
        //2.调用业务层获取支付页面
        return alipayService.createPlipayPage(orderId);
    }


    /**
     * 支付宝同步回调地址处理,重定向地址支付成功页面
     *
     * @param response
     */
    @GetMapping("/alipay/callback/return")
    public void redirectUrl(HttpServletResponse response) throws IOException {
        //重定向地址:http://payment.gmall.com/pay/success.html
        response.sendRedirect(AlipayConfig.return_order_url);
    }


    /**
     * 支付宝异步回调:通知商户系统支付结果
     *
     * @param paramsMap
     * @return
     */
    @PostMapping("/alipay/callback/notify")
    public String paySuccessNotify(@RequestParam Map paramsMap) {
        return alipayService.paySuccessNotify(paramsMap);
    }


    /**
     * 退款接口
     *
     * @param orderId
     * @return
     */
    @GetMapping("/alipay/refund/{orderId}")
    public Result refund(@PathVariable("orderId") Long orderId) {
        alipayService.refund(orderId);
        return Result.ok();
    }

    /**
     * 支付平台页面返回
     * @param orderId
     * @param payType
     * @return
     */
    @GetMapping("/paytest/{orderId}/{payType}")
    public Result payTest(@PathVariable("orderId") Long orderId, @PathVariable("payType") String payType){
        alipayService.payPage(payType, orderId);
        return Result.ok();
    }
}
